package com.gltqe.config;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.gltqe.cipher.*;
import com.gltqe.dict.DictBeanSerializerModifier;
import com.gltqe.dict.DictHandle;
import com.gltqe.dict.DictSerializer;
import com.gltqe.mask.DataMaskBeanSerializerModifier;
import com.gltqe.mask.DataMaskHandle;
import com.gltqe.mask.DataMaskSerializer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gltqe
 * @date 2023/6/16 9:01
 */
public interface JacksonCofig {
    /**
     * 配置jackson自定义序列化
     *
     * @return: java.lang.Object
     * @author gltqe
     * @date 2023/6/16 14:55
     **/
    Object jackson2ObjectMapperBuilderCustomizer();

    /**
     * 获取自定义序列化 简化jackson2ObjectMapperBuilderCustomizer配置
     *
     * @param handles 自定义处理器
     * @author gltqe
     * @date 2023/6/18 14:10
     * @return: java.util.List<com.fasterxml.jackson.databind.Module>
     */
    default List<Module> getSimpleModule(Handle... handles) {
        List<Module> list = new ArrayList<>();
        for (Handle handle : handles) {
            SimpleModule simpleModule = new SimpleModule();
            if (handle instanceof DictHandle) {
                simpleModule.setSerializerModifier(new DictBeanSerializerModifier(new DictSerializer((DictHandle) handle)));
            } else if (handle instanceof CipherHandle) {
                simpleModule.setSerializerModifier(new CipherBeanSerializerModifier(new CipherSerializer((CipherHandle) handle)));
                simpleModule.setDeserializerModifier(new CipherBeanDeserializerModifier(new CipherDeserializer((CipherHandle) handle)));
            } else if (handle instanceof DataMaskHandle) {
                simpleModule.setSerializerModifier(new DataMaskBeanSerializerModifier(new DataMaskSerializer((DataMaskHandle) handle)));
            }
            list.add(simpleModule);
        }
        return list;
    }
}
